package com.ruoyi.web.controller.coopHan;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.TAssociationClapDetail;
import com.ruoyi.system.domain.vo.coopHan.*;
import com.ruoyi.system.dto.coopHan.CulvertDto;
import com.ruoyi.system.dto.coopHan.CulvertHarmonizeDto;
import com.ruoyi.system.dto.coopHan.PageCulvertDto;
import com.ruoyi.system.dto.coopHan.SubmitDto;
import com.ruoyi.system.service.IAssociationClapCulvertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.rmi.ServerException;
import java.util.List;

/**
 * <p>
 * 协拍涵表 前端控制器
 * </p>
 *
 * @author yanfu
 * @since 2024-03-01
 */
@RestController
@RequestMapping("/association/clap/culvert")
@RequiredArgsConstructor
@Api(tags = {"协拍涵API"})
public class AssociationClapCulvertController extends BaseController {

    private final IAssociationClapCulvertService associationClapCulvertService;

/*    @PostMapping("/save")
    @ApiOperation(value = "协拍涵添加---手机端")
    public AjaxResult<CulvertVo> saveCulvert(@RequestBody CulvertDto dto) {
        CulvertVo culvertVo = associationClapCulvertService.saveCulvert(dto);
        return AjaxResult.success(culvertVo);
    }*/

    @PostMapping("/approval-page")
    @ApiOperation(value = "协拍涵办理申请列表")
    public AjaxResult<Page<PageCulvertApprovalVo>> getApprovalPage(@RequestBody PageCulvertDto dto) {
        Page<PageCulvertApprovalVo> culvertVo = associationClapCulvertService.getApprovalPage(dto);
        return AjaxResult.success(culvertVo);
    }
    // todo 协拍涵 开始

    /**
     * 协拍涵办理申请【受理】列表
     * @param dto
     * @return
     */
    @PostMapping("/acceptance-page")
    @ApiOperation(value = "协拍涵办理申请【受理】列表")
    public AjaxResult<Page<PageCulvertApprovalVo>> getAcceptancePage(@RequestBody PageCulvertDto dto) {
        Page<PageCulvertApprovalVo> culvertVo = associationClapCulvertService.getAcceptancePage(dto);
        return AjaxResult.success(culvertVo);
    }

    /**
     * 协拍涵办理申请【审查决定】列表
     * @param dto
     * @return
     */
    @PostMapping("/reviewDecision-page")
    @ApiOperation(value = "协拍涵办理申请【审查决定】列表")
    public AjaxResult<Page<PageCulvertApprovalVo>> getReviewDecisionPage(@RequestBody PageCulvertDto dto) {
        Page<PageCulvertApprovalVo> culvertVo = associationClapCulvertService.getReviewDecisionPage(dto);
        return AjaxResult.success(culvertVo);
    }

    /**
     * 协拍涵办理申请【盖章发函】列表
     * @param dto
     * @return
     */
    @PostMapping("/stampSendLetter-page")
    @ApiOperation(value = "协拍涵办理申请【盖章发函】列表")
    public AjaxResult<Page<PageCulvertApprovalVo>> getStampSendLetterPage(@RequestBody PageCulvertDto dto) {
        Page<PageCulvertApprovalVo> culvertVo = associationClapCulvertService.getStampSendLetterPage(dto);
        return AjaxResult.success(culvertVo);
    }
    // todo 协拍涵 结束



    @GetMapping("/approval-detail")
    @ApiOperation(value = "协拍涵办理申请-详情")
    public AjaxResult<CulvertApprovalDetailVo> getApprovalDetail(@RequestParam Long id) {
        CulvertApprovalDetailVo culvertVo = associationClapCulvertService.getApprovalDetail(id);
        return AjaxResult.success(culvertVo);
    }

    @DeleteMapping("/del")
    @ApiOperation(value = "协拍涵删除")
    public AjaxResult<String> delCulvert(@RequestParam Long id) throws ServerException {
        associationClapCulvertService.delCulvert(id);
        return AjaxResult.success("协拍涵删除成功");
    }

/*
    @PutMapping("/update")
    @ApiOperation(value = "协拍涵草稿编辑---手机端")
    public AjaxResult<String> putCulvert(@RequestBody CulvertDto dto) throws Exception {
        associationClapCulvertService.putCulvert(dto);
        return AjaxResult.success("协拍涵草稿编辑成功");
    }
*/

    @PostMapping("/page")
    @ApiOperation(value = "协拍涵管理列表 ")
    public AjaxResult<Page<PageCulvertVo>> pageCulvert(@RequestBody PageCulvertDto dto) throws Exception {
        Page<PageCulvertVo> page = associationClapCulvertService.pageCulvert(dto);
        return AjaxResult.success(page);
    }

    @GetMapping("/feedback")
    @ApiOperation(value = "协拍涵管理-反馈协调情况/详情")
    public AjaxResult<List<TAssociationClapDetail>> feedbackCulvert(@RequestParam Long id) throws Exception {
        List<TAssociationClapDetail> list = associationClapCulvertService.feedbackCulvert(id);
        return AjaxResult.success(list);
    }

    @PostMapping("/harmonize")
    @ApiOperation(value = "协拍涵管理-反馈协调情况-协调接口/单个协调/全部协调")
    public AjaxResult<String> harmonizeCulvert(@Validated @RequestBody CulvertHarmonizeDto dto) throws Exception {
        List<Long> ids = dto.getIds();
        associationClapCulvertService.harmonizeCulvert(ids);
        return AjaxResult.success("协调成功");
    }

    @PostMapping("/export")
    @ApiOperation(value = "协拍涵清单导出")
    public void export(@Validated @RequestBody CulvertHarmonizeDto dto, HttpServletResponse response) throws IOException {
        List<CulvertApprovalDetailExportVo> export = associationClapCulvertService.export(dto);
        // 导出参数设置
        ExportParams exportParams = new ExportParams("重庆市影视拍摄协调服务函", "协拍涵");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, CulvertApprovalDetailExportVo.class, export);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("culvert_approval_detail.xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
        try (ServletOutputStream out = response.getOutputStream()) {
            workbook.write(out);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    ((AutoCloseable) workbook).close();
                } catch (Exception ex) {
                    // 忽略关闭异常
                }
            }

        }

    }
}